﻿using Autofac;
using Autofac.Integration.WebApi;
using IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Routing;

namespace EnvirProtection
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //注册log4net
            log4net.Config.XmlConfigurator.Configure();
            //注册autofac
            InitAutoFac();          
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
        private void InitAutoFac()
        {
            var configuration = GlobalConfiguration.Configuration;
            var builder = new ContainerBuilder();
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            var services = Assembly.Load("Service");
            builder.RegisterAssemblyTypes(services)
                .Where(type => !type.IsAbstract && typeof(IServiceSupport).IsAssignableFrom(type))
                .AsImplementedInterfaces().SingleInstance().PropertiesAutowired();
            var container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            configuration.DependencyResolver = resolver;
        }
    }
}
